<template>
  <div class="favorites-container">
    <el-card class="favorites-card">
      <template #header>
        <div class="card-header">
          <h2>我的收藏</h2>
          <p>您收藏的所有景点</p>
        </div>
      </template>
      
      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="3" animated />
        <el-skeleton :rows="3" animated />
        <el-skeleton :rows="3" animated />
      </div>
      
      <div v-else-if="favorites.length === 0" class="empty-state">
        <el-empty description="暂无收藏的景点">
          <el-button type="primary" @click="goToAttractions">去浏览景点</el-button>
        </el-empty>
      </div>
      
      <div v-else class="favorites-list">
        <el-row :gutter="20">
          <el-col 
            v-for="favorite in favorites" 
            :key="favorite.id"
            :xs="24" 
            :sm="12" 
            :md="8" 
            :lg="6"
          >
            <el-card class="favorite-card" :body-style="{ padding: '0px' }">
              <img :src="favorite.imageUrl || require('@/assets/placeholder.jpg')" class="favorite-image" />
              <div class="favorite-info">
                <h4>{{ favorite.name }}</h4>
                <div class="favorite-meta">
                  <span>
                    <el-icon><Location /></el-icon>
                    {{ favorite.location }}
                  </span>
                  <span>
                    <el-icon><Star /></el-icon>
                    {{ favorite.rating || '暂无评分' }}
                  </span>
                </div>
                <div class="favorite-actions">
                  <el-button 
                    type="primary" 
                    size="small" 
                    @click="viewAttraction(favorite.id)"
                  >
                    查看详情
                  </el-button>
                  <el-button 
                    type="danger" 
                    size="small" 
                    @click="removeFavoriteItem(favorite.id)"
                    :loading="removingId === favorite.id"
                  >
                    取消收藏
                  </el-button>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
        
        <div class="pagination-container">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[8, 16, 24, 32]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Location, Star } from '@element-plus/icons-vue';
import { getUserFavorites, removeFavorite } from '@/api/user';

const router = useRouter();
const loading = ref(false);
const favorites = ref([]);
const currentPage = ref(1);
const pageSize = ref(8);
const total = ref(0);
const removingId = ref(null);

// 获取收藏列表
const fetchFavorites = async () => {
  loading.value = true;
  try {
    const response = await getUserFavorites({
      page: currentPage.value,
      size: pageSize.value
    });
    
    if (response.code === 200 && response.data) {
      favorites.value = response.data.records || [];
      total.value = response.data.total || 0;
    } else {
      ElMessage.error('获取收藏列表失败');
    }
  } catch (error) {
    console.error('获取收藏列表出错:', error);
    ElMessage.error('获取收藏列表失败，请稍后再试');
  } finally {
    loading.value = false;
  }
};

// 查看景点详情
const viewAttraction = (id) => {
  router.push(`/attraction/${id}`);
};

// 取消收藏
const removeFavoriteItem = async (id) => {
  try {
    await ElMessageBox.confirm(
      '确定要取消收藏这个景点吗？',
      '取消收藏',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    );
    
    removingId.value = id;
    
    try {
      const response = await removeFavorite(id);
      
      if (response.code === 200) {
        ElMessage.success('已取消收藏');
        // 重新获取收藏列表
        fetchFavorites();
      } else {
        ElMessage.error(response.message || '取消收藏失败');
      }
    } catch (error) {
      console.error('取消收藏出错:', error);
      ElMessage.error('取消收藏失败，请稍后再试');
    } finally {
      removingId.value = null;
    }
  } catch {
    // 用户取消操作
  }
};

// 前往景点页面
const goToAttractions = () => {
  router.push('/attractions');
};

// 处理分页
const handleSizeChange = (size) => {
  pageSize.value = size;
  fetchFavorites();
};

const handleCurrentChange = (page) => {
  currentPage.value = page;
  fetchFavorites();
};

// 页面加载时获取收藏列表
onMounted(() => {
  fetchFavorites();
});
</script>

<style scoped>
.favorites-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

.favorites-card {
  margin-bottom: 20px;
}

.card-header {
  margin-bottom: 20px;
}

.card-header h2 {
  margin: 0 0 5px;
  font-size: 24px;
}

.card-header p {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.loading-container {
  padding: 20px;
}

.empty-state {
  padding: 40px 0;
  display: flex;
  justify-content: center;
}

.favorites-list {
  margin-top: 20px;
}

.favorite-card {
  margin-bottom: 20px;
  transition: all 0.3s;
  height: 100%;
}

.favorite-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.favorite-image {
  width: 100%;
  height: 180px;
  object-fit: cover;
}

.favorite-info {
  padding: 15px;
}

.favorite-info h4 {
  margin: 0 0 10px;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.favorite-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  color: #909399;
  font-size: 14px;
}

.favorite-meta span {
  display: flex;
  align-items: center;
}

.favorite-meta .el-icon {
  margin-right: 5px;
}

.favorite-actions {
  display: flex;
  justify-content: space-between;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

@media (max-width: 768px) {
  .favorites-container {
    padding: 0 10px;
  }
}
</style>